﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using Org.Eurekaa.DNJ.Helpers;
using Org.Eurekaa.DNJ.UI.DNJSettings;
using Org.Eurekaa.DNJ.Settings;

namespace Org.Eurekaa.DNJ.UI.WebControls
{

    [ToolboxBitmap(typeof(Org.Eurekaa.DNJ.UI.Resources.icons.DNJUIIcons), "DNJAccordion.bmp")]
    [ToolboxData("<{0}:DNJAccordion runat=\"server\" Width=\"100%\" ></{0}:DNJAccordion>")]
    [Designer(typeof(Org.Eurekaa.DNJ.UI.Design.WebControls.DNJAccordionControlDesigner))]
    public class DNJAccordion : DNJMultiboxControl
    {
        private DNJAccordionSettings _settings = new DNJAccordionSettings();

        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Settings")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]        
        public DNJAccordionSettings Settings
        {
            get { return _settings; }
            set { _settings = value; }
        }

        private DNJAccordionEvents _events = new DNJAccordionEvents();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Events")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DNJAccordionEvents ClientEvents
        {
            get { return _events; }
            set { _events = value; }
        }

        public override void SaveSettings(ref object stateObject)
        {
            object[] lstObj = new object[2];
            lstObj[0] = Settings;
            lstObj[1] = ClientEvents;

            stateObject = lstObj;
            GenericSettingsObject allSettings = new GenericSettingsObject();


            PageHelper.AppendJQueryScript(ClientEvents.GetDefaultEventsScript(), this.Page.Header);

            allSettings.Append(Settings);
            allSettings.Append(ClientEvents);

            stateObject = Settings;
            PageHelper.AppendJQueryPlugin("#" + this.ClientID, "accordion", Settings.ToString(), this.Page.Header);
        }
        public override void LoadSettings(object stateObject)
        {
            //Settings = (stateObject as DNJAccordionSettings);
            object[] lstObj = (object[])stateObject;
            Settings = (lstObj[0] as DNJAccordionSettings);
            ClientEvents = (lstObj[1] as DNJAccordionEvents);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();


            if (BoxList.Count == 0 && DesignMode)
                this.Controls.Add(new LiteralControl("Accordion is empty, click here and add a page"));
                
            int i = 0;
            foreach (DNJBox pane in BoxList)
            {
                HtmlGenericControl h3 = new HtmlGenericControl("h3");
                HtmlAnchor a = new HtmlAnchor();
                a.HRef = "#" + this.ClientID +  pane.ClientID;
                a.InnerHtml = pane.Title;
                HtmlGenericControl div = new HtmlGenericControl("div");
                div.Attributes.Add("id", this.ClientID + pane.ClientID);

                if (DesignMode)
                {
                    string state = (i == CurrentDesignBox) ? "active" : "default";
                    h3.Attributes.Add("class", "ui-accordion-header ui-helper-reset ui-state-" + state + " ui-corner-all");
                    h3.Attributes.Add("style", "padding:0px;margin:0px;height:30px;position:relative");
                    //a.Attributes.Add("style", "position:absolute;top:5px;left:5px");
                    if (i != CurrentDesignBox)
                    {
                        div.Visible = false;
                    }
                    else
                    {
                        div.Attributes.Add("class", "ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active");
                        div.Attributes.Add("style", "border:3px outset;padding:10px 5px");
                        div.Style.Add("height", (new Unit(this.Height.Value - BoxList.Count * 30 - 30)).ToString());
                    }

                    //if (i != CurrentDesignBox)
                    //    div.Style.Add("display", "none");

                }

                if (pane.Content != null)
                    pane.Content.InstantiateIn(div);
                                
                h3.Controls.Add(a);
                Controls.Add(h3);
                Controls.Add(div);
                i++;
            }

            
                

            
        }

        //#endregion implementations
    }
}
